package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDDMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealDDMapper setmealDDMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSeteaml(SetmealDTO setmealDTO) {
        log.info("新增套餐信息：{}",setmealDTO);
        //将setmeal的数据赋值给setmealDTO
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //新增套餐信息方法
        setmealMapper.saveSetmeal(setmeal);
        //新增套餐前还需要查新菜品信息 获取菜品套餐分类id将菜品信息遍历出来
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
/*        setmealDishes.forEach(setmealDish ->
                setmealDish.setSetmealId(setmealId);
                 setmealDDMapper.saveSetmealDish(setmealDishes);
        );*/
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDDMapper.saveSetmealDish(setmealDish);
        }

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult getpage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询：{}",setmealPageQueryDTO);
        //开启分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
//        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());/*/
        Page<SetmealVO> page=setmealMapper.getpage(setmealPageQueryDTO);
        //获取总页数
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total,result);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deteleById(List<Long> ids) {
        log.info("根据套餐起售状态删除套餐：{}",ids);
        //判断套餐里面的菜品是否是起售状态，若是则不能删除
        for (Long id : ids) {
            //获取套餐id
            Setmeal setmeal=setmealMapper.getid(id);
            //判断套餐菜品状态,处于起售状态不能删除
            if(setmeal.getStatus()== StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

        }
        for (Long aid : ids) {
            //删除
            setmealMapper.deteleSeteamlId(aid);
            setmealDDMapper.deteleSeteamlDishId(aid);
        }

    }

    /**
     * 查询套餐id 查询套餐菜品关系表 后续修改套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO setele(Long id) {
        //根据id查询套餐信息
     SetmealVO setmeal=setmealMapper.setele(id);
     //根据套餐id获取套餐菜品数据
        List<SetmealDish> setmealdish = setmealDDMapper.getId(id);
        //将查询数据封装到setmealVO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealdish);
        return setmealVO;
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        log.info("根据套餐id修改套餐信息:{}",setmealDTO);
        //将查询到的信息赋值给setmealDTO
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updatesetmeal(setmeal);
        //获取套餐id
        Long setmealid = setmeal.getId();
        //查询套餐与菜品表的关系，删除套餐与菜品关联关系
        setmealDDMapper.deteleSeteamlDishId(setmealid);
        //修改套餐里的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish ->{
                    setmealDish.setSetmealId(setmealid);
                });
        setmealDDMapper.updateSeteamlDish(setmealDishes);
    }

    /**
     * 开启起售和禁售状态
     * @param status
     * @param id
     */
    @Override
    public void startorstopstaus(Integer status, Long id) {
        //判断套餐里是否有停售菜品，有停售菜品，当开启套餐是应该提示该套餐有停售菜品不能开启套餐
        if(status==StatusConstant.ENABLE){//状态未起售，获得菜品对应id
            List<Dish> list=dishMapper.getBySetmealId(id);
            if(list!=null&&list.size()>0){
                list.forEach(dish -> {
                    //如果菜品状态未0，抛出套餐中包含停售菜品不能起售
                    if(StatusConstant.DISABLE==dish.getStatus()) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                    });
                }
            }
        //创建setmeal对象
        Setmeal setmeal = new Setmeal();
        //获取其status和id
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.startorstopstaus(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}





